 /* nginx 对于单向链表的封装   
  *
  *
  *
  * 
  * Copyright (C) Igor Sysoev
  * Copyright (C) Nginx, Inc.
  */


#ifndef _NGX_LIST_H_INCLUDED_
#define _NGX_LIST_H_INCLUDED_


#include <ngx_config.h>
#include <ngx_core.h>


typedef struct ngx_list_part_s  ngx_list_part_t;

/*@brief:链表元素的结构体
 *
 *
 */
struct ngx_list_part_s {
    void             *elts; //列表元素value 
    ngx_uint_t        nelts; //
    ngx_list_part_t  *next; //指向下一个元素
};


/*@brief:链表上下文结构体
 *
 *
 */
typedef struct {
    ngx_list_part_t  *last; //链表的头结点
    ngx_list_part_t   part;
    size_t            size;  //链表每个节点的大小
    ngx_uint_t        nalloc; 
    ngx_pool_t       *pool; //内存池;
} ngx_list_t;


ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size);

/*@brief: 链表初始化 
 *@para: ngx_list_t *list:链表上下文 
 *		 ngx_pool_t *pool:内存池 
 *		 ngx_uint_t n:
 *		 size_t size:
 *@return value: NGX_OK:1
 */
static ngx_inline ngx_int_t ngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size)
{
    list->part.elts = ngx_palloc(pool, n * size); //分配一个内存块,
    if (list->part.elts == NULL) {
        return NGX_ERROR;
    }

    list->part.nelts = 0;  //当前已经被使用的节点个数
    list->part.next = NULL;  //
    list->last = &list->part; //链表头节点 
    list->size = size; //每个节点的大小
    list->nalloc = n;  //链表的节点个数
    list->pool = pool; //所在的内存池;

    return NGX_OK;
}


/*
 *
 *  the iteration through the list:
 *
 *  part = &list.part;
 *  data = part->elts;
 *
 *  for (i = 0 ;; i++) {
 *
 *      if (i >= part->nelts) {
 *          if (part->next == NULL) {
 *              break;
 *          }
 *
 *          part = part->next;
 *          data = part->elts;
 *          i = 0;
 *      }
 *
 *      ...  data[i] ...
 *
 *  }
 */


void *ngx_list_push(ngx_list_t *list);


#endif /* _NGX_LIST_H_INCLUDED_ */
